package com.gexiaobao.pigeon.ui.fragment.mine.organization.video

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPlaybackBinding
import com.gexiaobao.pigeon.ui.adapter.VideoFifteenAdapter
import com.gexiaobao.pigeon.ui.fragment.mine.pigeon.activity.ActivityVideoPlayer
import com.gexiaobao.pigeon.viewmodel.VideoViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity
import kotlin.reflect.typeOf

/**
 * @Author      : hxw
 * @Date        : 2024/8/24 14:37
 * @Describe    : 15视频
 */
class ActivityPlayBack : BaseActivity<VideoViewModel, ActivityPlaybackBinding>() {

    private var passId = 0
    private var ringId = ""
    private var gatherId = 0
    private val mAdapter: VideoFifteenAdapter by lazy { VideoFifteenAdapter(arrayListOf()) }
    private var mUrlLive = ""
    private var mType = 0

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mType = intent.getIntExtra("type", 0)
        val title = intent.getStringExtra("title")
        mDatabind.includeBar.tvTitle.text = title
        if (mType == 1) {
            gatherId = intent.getIntExtra("gatherId", 0)
            mDatabind.tvContent.text = getString(R.string.gather_video_announcement_of_the_organizer)
        } else {
            mDatabind.tvContent.text = getString(R.string.back_video_announcement_of_the_organizer)
            ringId = intent.getStringExtra("ringId").toString()
            passId = intent.getIntExtra("passId", 0)
        }

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
        onRefresh()

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            mUrlLive = mAdapter.data[position].video
            startActivity<ActivityVideoPlayer>(
                "videoUrl" to mUrlLive,
                "title" to mAdapter.data[position].cameraNo + getString(R.string.camera_number),
                "type" to "videoBackAndGather",
                "featherColor" to mAdapter.data[position].featherColor,
                "fosterName" to mAdapter.data[position].fosterName,
                "passName" to mAdapter.data[position].passName,
                "ringId" to mAdapter.data[position].ringId,
                "showTime" to mAdapter.data[position].showTime,
                "coverImage" to mAdapter.data[position].snapshot,
            )
        }
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        if (mType == 1) {
            mViewModel.getGatherVideo(gatherId)
        } else {
            mViewModel.getBackVideo(ringId, passId)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.getGatherVideoResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            mAdapter.setList(it.videos)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}